
const pool = require('./config/db');

const app = require('./app');
// 从config.js中读取端口号
const config = require('./config');
// 使用config中的端口号
const PORT = config.port;

// 应用关闭时结束连接池
process.on('SIGINT', async () => {
    console.log('关闭数据库连接池...');
    await pool.end();
    process.exit(0);
});

async function startServer() {
    try {
        // 测试连接池
        const [result] = await pool.query('SELECT 1 + 1 AS solution');
        console.log(`数据库连接测试: 1 + 1 = ${result[0].solution}`);
        
        // 启动服务器
        app.listen(PORT, () => {
            console.log(`服务器运行在 http://localhost:${PORT}`);
            console.log('数据库信息:');
            console.log(`- 地址: ${config.db.host}`);
            console.log(`- 数据库: ${config.db.database}`);
            console.log(`- 用户: ${config.db.user}`);
        });
    } catch (error) {
        console.error('服务器启动失败:', error);
        process.exit(1);
    }
}

startServer();

